﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.ComponentModel;
using System.Collections;

using Willson.JsbManager.Idal;
using Willson.JsbManager.Entity;
using Willson.JsbManager.BllEntity;
using Willson.JsbManager.Comm;

namespace Willson.JsbManager.Bll
{
    public class BLLCompanyJdPrint:BaseBLL
    {
        private ITableXtCompany xt_company_DAL = null;
        private ITableXtSbJd xt_sb_jd_DAL = null;
        private ITableXtCompany_sb_info xt_company_sb_info_DAL = null;

        public BLLCompanyJdPrint()
        {
            xt_company_DAL = Reflect<ITableXtCompany>.Create("DAL_table_xt_company", "Willson.JsbManager.Dal");
            xt_sb_jd_DAL = Reflect<ITableXtSbJd>.Create("DAL_table_xt_sb_jd", "Willson.JsbManager.Dal");
            xt_company_sb_info_DAL = Reflect<ITableXtCompany_sb_info>.Create("DAL_table_xt_company_sb_info", "Willson.JsbManager.Dal");
        }
        
        public List<BECompanyLetterPrint> GetCompanyLetterPrintInfoList()
        {
            List<BECompanyLetterPrint> list = new List<BECompanyLetterPrint>();

            Hashtable hash = new Hashtable();

            hash.Add("remove", 0);

            IEnumerator<TableXtCompanyEntity> ator = xt_company_DAL.Find(hash, SqlOperator.And, true).GetEnumerator();

            hash.Clear();

            while (ator.MoveNext())
            {
                BECompanyLetterPrint info = new BECompanyLetterPrint();
                info.m_PostCode = ator.Current.Code;
                info.m_Address = ator.Current.Address;
                info.m_Sjr = ator.Current.Name + ator.Current.Sjr + "收";

                list.Add(info);
            }

            return list;
        }

        public List<BECompanyJdPrint> PrintJd(int year, int jd)
        {
            List<BECompanyJdPrint> list = new List<BECompanyJdPrint>();

            int index = 1;
            int li_sn_jh = 0;
            int li_sn_ysl = 0;
            int li_bq_jh = 0;

            Hashtable hash = new Hashtable();

            hash.Add("remove", 0);

            IEnumerator<TableXtCompanyEntity> ator = xt_company_DAL.Find(hash, SqlOperator.And, true).GetEnumerator();

            hash.Clear();

            while (ator.MoveNext())
            {
                BECompanyJdPrint info = new BECompanyJdPrint();

                info.m_Company_name = ator.Current.Name;

                hash.Add("company_id", ator.Current.Id);

                IEnumerator<TableXtCompanySbInfoEntity> ator1 = xt_company_sb_info_DAL.Find(hash, SqlOperator.And, true).GetEnumerator();

                hash.Clear();

                index = 0;

                while (ator1.MoveNext())
                {
                    index++;
                    
                    if (index == 1)
                    {
                        info.m_User_code_1 = ator1.Current.User_code;
                        info.m_Sn_jh_1 = "合计";
                        info.m_Sn_ysl_1 = "合计";
                        info.m_Bq_jh_1 = "合计";
                    }

                    if (index == 2)
                    {
                        info.m_User_code_2 = ator1.Current.User_code;
                        info.m_Sn_jh_2 = "合计";
                        info.m_Sn_ysl_2 = "合计";
                        info.m_Bq_jh_2 = "合计";
                    }

                    if (index == 3)
                    {
                        info.m_User_code_3 = ator1.Current.User_code;
                        info.m_Sn_jh_3 = "合计";
                        info.m_Sn_ysl_3 = "合计";
                        info.m_Bq_jh_3 = "合计";
                    }

                    if (index == 4)
                    {
                        info.m_User_code_4 = ator1.Current.User_code;
                        info.m_Sn_jh_4 = "合计";
                        info.m_Sn_ysl_4 = "合计";
                        info.m_Bq_jh_4 = "合计";
                    }

                    if (index == 5)
                    {
                        info.m_User_code_5 = ator1.Current.User_code;
                        info.m_Sn_jh_5 = "合计";
                        info.m_Sn_ysl_5 = "合计";
                        info.m_Bq_jh_5 = "合计";
                    }

                    if (index == 6)
                    {
                        info.m_User_code_6 = ator1.Current.User_code;
                        info.m_Sn_jh_6 = "合计";
                        info.m_Sn_ysl_6 = "合计";
                        info.m_Bq_jh_6 = "合计";
                    }

                    
                }

                hash.Add("company_id", ator.Current.Id);
                hash.Add("sb_year", year -1);
                hash.Add("sb_jd", jd);

                TableXtSbJdEntity jd_entity_old = xt_sb_jd_DAL.Find(hash);

                hash.Clear();

                hash.Add("company_id", ator.Current.Id);
                hash.Add("sb_year", year);
                hash.Add("sb_jd", jd);

                TableXtSbJdEntity jd_entity_new = xt_sb_jd_DAL.Find(hash);

                hash.Clear();

                if (jd_entity_old == null)
                {
                    li_sn_jh = 0;
                    li_sn_ysl = 0;
                }
                else
                {
                    if (jd_entity_old.Jd_jh_begin > 0)
                        li_sn_jh = jd_entity_old.Jd_jh_begin;
                    else
                        li_sn_jh = jd_entity_old.Jd_jh;

                    li_sn_ysl = jd_entity_old.Jd_sum;
                }

                if (jd_entity_new == null) continue;

                if (jd_entity_new.Jd_jh_begin > 0)
                    li_bq_jh = jd_entity_new.Jd_jh_begin;
                else
                    li_bq_jh = jd_entity_new.Jd_jh;

                if (index == 1)
                {
                    info.m_Sn_jh_1 = li_sn_jh.ToString();
                    info.m_Sn_ysl_1 = li_sn_ysl.ToString();
                    info.m_Bq_jh_1 = li_bq_jh.ToString();
                }

                if (index == 2)
                {
                    info.m_Sn_jh_2 = li_sn_jh.ToString();
                    info.m_Sn_ysl_2 = li_sn_ysl.ToString();
                    info.m_Bq_jh_2 = li_bq_jh.ToString();
                }

                if (index == 3)
                {
                    info.m_Sn_jh_3 = li_sn_jh.ToString();
                    info.m_Sn_ysl_3 = li_sn_ysl.ToString();
                    info.m_Bq_jh_3 = li_bq_jh.ToString();
                }

                if (index == 4)
                {
                    info.m_Sn_jh_4 = li_sn_jh.ToString();
                    info.m_Sn_ysl_4 = li_sn_ysl.ToString();
                    info.m_Bq_jh_4 = li_bq_jh.ToString();
                }

                if (index == 5)
                {
                    info.m_Sn_jh_5 = li_sn_jh.ToString();
                    info.m_Sn_ysl_5 = li_sn_ysl.ToString();
                    info.m_Bq_jh_5 = li_bq_jh.ToString();
                }

                if (index == 6)
                {
                    info.m_Sn_jh_6 = li_sn_jh.ToString();
                    info.m_Sn_ysl_6 = li_sn_ysl.ToString();
                    info.m_Bq_jh_6 = li_bq_jh.ToString();
                }

                list.Add(info);

            }

            return list;
        }
    }
}
